/*
 * Copyright (c) 2015, 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sudworth.themvp.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewbinding.ViewBinding;

import java.lang.ref.SoftReference;

/**
 * View delegate base class
 * 视图层代理的基类
 * 如果app被意外杀死，重启app，AppDelegate会重新实例化，并不会复用之前的数据
 *
 * @author kymjs (http://www.kymjs.com/) on 10/23/15.
 */
public abstract class AppDelegate<B extends ViewBinding> implements IDelegate {

    protected View rootView;
    private SoftReference mActivity;
    private long mToastLastTime;
    private B b;
    private FragmentManager mFragmentManager;

    public abstract B onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container);

    public B getViewBinding() {
        return b;
    }

    @Override
    public void create(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        b = onCreateViewBinding(inflater, container);
        rootView = b.getRoot();
    }

    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    public Toolbar getToolbar() {
        return null;
    }

    @Override
    public void setFragmentManager(FragmentManager manager) {
        mFragmentManager = manager;
    }

    @Override
    public FragmentManager getFragmentManager() {
        return mFragmentManager;
    }

    @Override
    public View getRootView() {
        return rootView;
    }

    @Override
    public void initWidget() {
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {

    }

    @Override
    public void toast(CharSequence msg) {
        if(System.currentTimeMillis() - mToastLastTime < 500) return;
        mToastLastTime = System.currentTimeMillis();
        Toast.makeText(rootView.getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setActivity(Activity activity) {
        if (mActivity == null) {
            mActivity = new SoftReference<Activity>(activity);
        }
    }

    public @Nullable <T extends Activity> T getActivity() {
        return (T) mActivity.get();
    }

    public FragmentActivity requireActivity() {
        if(getActivity() == null) {
            throw new NullPointerException("activity == null, 页面已经被销毁");
        }
        return getActivity();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void destroy() {
        onDestroy();
        if (mActivity != null) {
            mActivity.clear();
        }
        rootView = null;
    }
}
